{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = pd.read_csv(\"../data/prices.csv\", encoding='gbk')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>代码</th>\n",
       "      <th>简称</th>\n",
       "      <th>日期</th>\n",
       "      <th>开盘价(元)</th>\n",
       "      <th>收盘价(元)</th>\n",
       "      <th>涨跌幅(%)</th>\n",
       "      <th>均价(元)</th>\n",
       "      <th>总市值(元)</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0700.HK</td>\n",
       "      <td>腾讯控股</td>\n",
       "      <td>2018/1/2</td>\n",
       "      <td>408.0</td>\n",
       "      <td>417.8</td>\n",
       "      <td>2.9064</td>\n",
       "      <td>415.7443</td>\n",
       "      <td>3968710000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0700.HK</td>\n",
       "      <td>腾讯控股</td>\n",
       "      <td>2018/1/3</td>\n",
       "      <td>424.0</td>\n",
       "      <td>422.2</td>\n",
       "      <td>1.0531</td>\n",
       "      <td>423.0984</td>\n",
       "      <td>4010500000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0700.HK</td>\n",
       "      <td>腾讯控股</td>\n",
       "      <td>2018/1/4</td>\n",
       "      <td>427.0</td>\n",
       "      <td>431.8</td>\n",
       "      <td>2.2738</td>\n",
       "      <td>428.8735</td>\n",
       "      <td>4101690000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0700.HK</td>\n",
       "      <td>腾讯控股</td>\n",
       "      <td>2018/1/5</td>\n",
       "      <td>436.4</td>\n",
       "      <td>433.2</td>\n",
       "      <td>0.3242</td>\n",
       "      <td>431.6058</td>\n",
       "      <td>4114990000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0700.HK</td>\n",
       "      <td>腾讯控股</td>\n",
       "      <td>2018/1/8</td>\n",
       "      <td>436.2</td>\n",
       "      <td>438.6</td>\n",
       "      <td>1.2465</td>\n",
       "      <td>436.6185</td>\n",
       "      <td>4166290000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        代码    简称        日期  开盘价(元)  收盘价(元)  涨跌幅(%)     均价(元)         总市值(元)\n",
       "0  0700.HK  腾讯控股  2018/1/2   408.0   417.8  2.9064  415.7443  3968710000000\n",
       "1  0700.HK  腾讯控股  2018/1/3   424.0   422.2  1.0531  423.0984  4010500000000\n",
       "2  0700.HK  腾讯控股  2018/1/4   427.0   431.8  2.2738  428.8735  4101690000000\n",
       "3  0700.HK  腾讯控股  2018/1/5   436.4   433.2  0.3242  431.6058  4114990000000\n",
       "4  0700.HK  腾讯控股  2018/1/8   436.2   438.6  1.2465  436.6185  4166290000000"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.DataFrame()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>stock</th>\n",
       "      <th>date</th>\n",
       "      <th>colsedPrice</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0700.HK</td>\n",
       "      <td>2018-01-02</td>\n",
       "      <td>417.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0700.HK</td>\n",
       "      <td>2018-01-03</td>\n",
       "      <td>422.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0700.HK</td>\n",
       "      <td>2018-01-04</td>\n",
       "      <td>431.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0700.HK</td>\n",
       "      <td>2018-01-05</td>\n",
       "      <td>433.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0700.HK</td>\n",
       "      <td>2018-01-08</td>\n",
       "      <td>438.6</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     stock       date  colsedPrice\n",
       "0  0700.HK 2018-01-02        417.8\n",
       "1  0700.HK 2018-01-03        422.2\n",
       "2  0700.HK 2018-01-04        431.8\n",
       "3  0700.HK 2018-01-05        433.2\n",
       "4  0700.HK 2018-01-08        438.6"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['stock'] = data['代码']\n",
    "df['date'] = pd.to_datetime(data['日期'])\n",
    "df['colsedPrice'] = data['收盘价(元)']\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>stock</th>\n",
       "      <th>000568.SZ</th>\n",
       "      <th>000858.SZ</th>\n",
       "      <th>002007.SZ</th>\n",
       "      <th>002032.SZ</th>\n",
       "      <th>0388.HK</th>\n",
       "      <th>0700.HK</th>\n",
       "      <th>1093.HK</th>\n",
       "      <th>1169.HK</th>\n",
       "      <th>1177.HK</th>\n",
       "      <th>2269.HK</th>\n",
       "      <th>300015.SZ</th>\n",
       "      <th>600009.SH</th>\n",
       "      <th>600161.SH</th>\n",
       "      <th>600519.SH</th>\n",
       "      <th>600809.SH</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2018-01-02</th>\n",
       "      <td>65.78</td>\n",
       "      <td>80.58</td>\n",
       "      <td>28.10</td>\n",
       "      <td>40.71</td>\n",
       "      <td>250.2</td>\n",
       "      <td>417.8</td>\n",
       "      <td>16.40</td>\n",
       "      <td>22.20</td>\n",
       "      <td>13.80</td>\n",
       "      <td>44.10</td>\n",
       "      <td>30.42</td>\n",
       "      <td>44.40</td>\n",
       "      <td>30.03</td>\n",
       "      <td>703.85</td>\n",
       "      <td>56.34</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-01-03</th>\n",
       "      <td>66.40</td>\n",
       "      <td>80.90</td>\n",
       "      <td>28.90</td>\n",
       "      <td>41.10</td>\n",
       "      <td>249.2</td>\n",
       "      <td>422.2</td>\n",
       "      <td>17.20</td>\n",
       "      <td>22.15</td>\n",
       "      <td>13.90</td>\n",
       "      <td>44.45</td>\n",
       "      <td>30.25</td>\n",
       "      <td>44.05</td>\n",
       "      <td>30.04</td>\n",
       "      <td>715.86</td>\n",
       "      <td>56.67</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-01-04</th>\n",
       "      <td>68.75</td>\n",
       "      <td>82.99</td>\n",
       "      <td>28.99</td>\n",
       "      <td>41.35</td>\n",
       "      <td>254.6</td>\n",
       "      <td>431.8</td>\n",
       "      <td>17.60</td>\n",
       "      <td>22.50</td>\n",
       "      <td>14.14</td>\n",
       "      <td>48.00</td>\n",
       "      <td>29.67</td>\n",
       "      <td>44.10</td>\n",
       "      <td>30.09</td>\n",
       "      <td>737.07</td>\n",
       "      <td>58.91</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-01-05</th>\n",
       "      <td>68.05</td>\n",
       "      <td>82.68</td>\n",
       "      <td>28.60</td>\n",
       "      <td>41.11</td>\n",
       "      <td>253.2</td>\n",
       "      <td>433.2</td>\n",
       "      <td>17.46</td>\n",
       "      <td>22.45</td>\n",
       "      <td>13.80</td>\n",
       "      <td>50.40</td>\n",
       "      <td>31.50</td>\n",
       "      <td>44.15</td>\n",
       "      <td>29.76</td>\n",
       "      <td>738.36</td>\n",
       "      <td>58.45</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-01-08</th>\n",
       "      <td>68.45</td>\n",
       "      <td>82.20</td>\n",
       "      <td>28.47</td>\n",
       "      <td>40.59</td>\n",
       "      <td>262.0</td>\n",
       "      <td>438.6</td>\n",
       "      <td>17.76</td>\n",
       "      <td>23.45</td>\n",
       "      <td>14.10</td>\n",
       "      <td>49.25</td>\n",
       "      <td>33.63</td>\n",
       "      <td>43.03</td>\n",
       "      <td>29.44</td>\n",
       "      <td>752.13</td>\n",
       "      <td>57.36</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "stock       000568.SZ  000858.SZ  002007.SZ  002032.SZ  0388.HK  0700.HK  \\\n",
       "date                                                                       \n",
       "2018-01-02      65.78      80.58      28.10      40.71    250.2    417.8   \n",
       "2018-01-03      66.40      80.90      28.90      41.10    249.2    422.2   \n",
       "2018-01-04      68.75      82.99      28.99      41.35    254.6    431.8   \n",
       "2018-01-05      68.05      82.68      28.60      41.11    253.2    433.2   \n",
       "2018-01-08      68.45      82.20      28.47      40.59    262.0    438.6   \n",
       "\n",
       "stock       1093.HK  1169.HK  1177.HK  2269.HK  300015.SZ  600009.SH  \\\n",
       "date                                                                   \n",
       "2018-01-02    16.40    22.20    13.80    44.10      30.42      44.40   \n",
       "2018-01-03    17.20    22.15    13.90    44.45      30.25      44.05   \n",
       "2018-01-04    17.60    22.50    14.14    48.00      29.67      44.10   \n",
       "2018-01-05    17.46    22.45    13.80    50.40      31.50      44.15   \n",
       "2018-01-08    17.76    23.45    14.10    49.25      33.63      43.03   \n",
       "\n",
       "stock       600161.SH  600519.SH  600809.SH  \n",
       "date                                         \n",
       "2018-01-02      30.03     703.85      56.34  \n",
       "2018-01-03      30.04     715.86      56.67  \n",
       "2018-01-04      30.09     737.07      58.91  \n",
       "2018-01-05      29.76     738.36      58.45  \n",
       "2018-01-08      29.44     752.13      57.36  "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pivotedDf = pd.pivot_table(df, values='colsedPrice', index=['date'], columns=['stock'])\n",
    "pivotedDf.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "pivotedDf.to_csv(\"../data/parsedData.csv\", index=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "ML",
   "language": "python",
   "name": "ml"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
